package com.scm.service.impl;

import com.scm.dao.PayRecordMapper;
import com.scm.dao.PomainMapper;
import com.scm.dao.SomainMapper;
import com.scm.entity.PayRecord;
import com.scm.entity.Pomain;
import com.scm.entity.Somain;
import com.scm.service.IPaymentService;
import com.scm.util.DateUtil;
import java.io.PrintStream;
import java.util.HashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class PaymentServiceImpl
        implements IPaymentService
{
    @Autowired
    private PomainMapper pomainMapper;
    @Autowired
    private SomainMapper somainMapper;
    @Autowired
    private PayRecordMapper payRecordMapper;

    @Transactional
    @Override
    public void pay(long poId, String account, String type)
    {
        Pomain pomain = this.pomainMapper.selectByPoId(Long.valueOf(poId));

        PayRecord payRecord = new PayRecord();
        payRecord.setAccount(account);
        payRecord.setOrdercode(String.valueOf(poId));
        payRecord.setPayTime(DateUtil.currentTime());

        HashMap<String, Object> map = new HashMap();
        map.put("poId", Long.valueOf(poId));
        map.put("account", account);
        map.put("now", DateUtil.currentTime());
        int r = 0;
        if ("1".equals(type))
        {
            r = this.pomainMapper.pay(map);
            payRecord.setPayType(Integer.valueOf(2));
            if (3 == pomain.getPayType().intValue()) {
                payRecord.setPayPrice(Double.valueOf(pomain.getPoTotal().doubleValue() - pomain.getPrePayFee().doubleValue()));
            } else {
                payRecord.setPayPrice(pomain.getPoTotal());
            }
        }
        else if ("2".equals(type))
        {
            r = this.pomainMapper.prepay(map);
            payRecord.setPayType(Integer.valueOf(4));
            payRecord.setPayPrice(pomain.getPrePayFee());
        }
        else
        {
            throw new RuntimeException("付款登记类型比匹配" + type);
        }
        if (r != 1) {
            throw new RuntimeException("付款登录操作失败" + type);
        }
        this.payRecordMapper.insert(payRecord);
    }

    @Transactional
    @Override
    public void rece(Long soId, String account, String type)
    {
        Somain somain = this.somainMapper.selectBySoId(soId);

        PayRecord payRecord = new PayRecord();
        payRecord.setAccount(account);
        payRecord.setOrdercode(String.valueOf(soId));
        payRecord.setPayTime(DateUtil.currentTime());

        HashMap<String, Object> map = new HashMap();
        map.put("soId", soId);
        map.put("account", account);
        map.put("now", DateUtil.currentTime());
        System.out.println(map + ".........");
        int r = 0;
        if ("1".equals(type))
        {
            r = this.somainMapper.pay(map);
            payRecord.setPayType(Integer.valueOf(1));
            if (3 == somain.getPayType().intValue()) {
                payRecord.setPayPrice(Double.valueOf(somain.getSoTotal().doubleValue() - somain.getPrePayFee().doubleValue()));
            } else {
                payRecord.setPayPrice(somain.getSoTotal());
            }
        }
        else if ("2".equals(type))
        {
            r = this.somainMapper.prepay(map);
            payRecord.setPayType(Integer.valueOf(3));
            payRecord.setPayPrice(somain.getPrePayFee());
        }
        else
        {
            throw new RuntimeException("收款登记类型不匹配" + type);
        }
        if (r != 1) {
            throw new RuntimeException("该销售单无法完成收款，操作失败");
        }
        this.payRecordMapper.insert(payRecord);
    }
}
